package com.axeiya.cheddargetter;

import java.util.Date;
import java.util.HashMap;

import org.jdom.Element;

public class Item {
	
	/**
	 * Your code for this tracked item. Limited to 36 characters.
	 * REQUIRED
	 */
	private String itemCode;
	
	/**
	 * The positive amount accurate to up to 4 decimal places that you wish to set the current usage to for this item. Can be zero.
	 * REQUIRED
	 */
	private int quantity;
	
	
	/**
	 * READ PARAMETERS
	 */
	private String name;
	
	private boolean isPeriodic;
	
	private float overageAmount;
	
	private Date createdDateTime;
	
	public Item(String itemCode, int quantity){
		this.itemCode = itemCode;
		this.quantity = quantity;
	}
	

	public Item(String itemCode, int quantity, String name,
			boolean isPeriodic, float overageAmount, Date createdDateTime) {
		super();
		this.itemCode = itemCode;
		this.quantity = quantity;
		this.name = name;
		this.isPeriodic = isPeriodic;
		this.overageAmount = overageAmount;
		this.createdDateTime = createdDateTime;
	}

	public static Item parseItem(Element item){
		String itemCode = item.getChildText("code");
		String itemName = item.getChildText("name");
		int itemQuantity = Integer.valueOf(item.getChildText("quantityIncluded"));
		boolean itemIsPeriodic = CGTools.parseBoolean(item.getChildText("isPeriodic"));
		float itemOverageAmount = Float.parseFloat(item.getChildText("overageAmount"));
		Date itemCreatedDatetime = CGTools.parseDateTime(item.getChildText("createdDateTime"));
		
		return new Item(itemCode, itemQuantity, itemName, itemIsPeriodic, itemOverageAmount, itemCreatedDatetime);
	}

	public static HashMap<String, String> formatItem(Item item){
		HashMap<String, String> parameters = new HashMap<String, String>();
		
		if(item.getItemCode() != null && !item.getItemCode().isEmpty()){
			parameters.put("itemCode", item.getItemCode());
		}else{
			//TODO: throw Required Exception
		}
		
		if(item.getQuantity() >= 0){
			parameters.put("quantity", String.valueOf(item.getQuantity()));
		}
		else{
			//TODO: throw Required positive Exception
		}
		
		return parameters;
	}

	public String getItemCode() {
		return itemCode;
	}

	public void setItemCode(String itemCode) {
		this.itemCode = itemCode;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}



	public boolean isPeriodic() {
		return isPeriodic;
	}

	public void setPeriodic(boolean isPeriodic) {
		this.isPeriodic = isPeriodic;
	}

	public float getOverageAmount() {
		return overageAmount;
	}

	public void setOverageAmount(float overageAmount) {
		this.overageAmount = overageAmount;
	}

	public Date getCreatedDateTime() {
		return createdDateTime;
	}

	public void setCreatedDateTime(Date createdDateTime) {
		this.createdDateTime = createdDateTime;
	}
	
	
	
	
}
